<template>
  <div class="custom_groupcells" ref="custom_groupcells">
    <div class="custom_groupcells_content" v-if="listData.length > 0">
      <lazy-component>
        <van-cell-group
          :title="item.title"
          v-for="(item, index) in listData"
          :key="'groupcells_index_' + index"
          :ref="'groupcells_index_' + index"
        >
          <template #title>
            <van-sticky
              :offset-top="46 / 23.44 + 'rem'"
              :container="$refs['custom_groupcells']"
            >
              <div class="custom_groupcells_content_title">
                <div class="custom_groupcells_content_title_left">
                  {{ item.title }}
                </div>
                <div
                  class="custom_groupcells_content_title_right"
                  @click="clearItemList(item, index)"
                >
                  <van-icon name="delete-o" />{{ $t("lang.清空") }}
                </div>
              </div>
            </van-sticky>
          </template>
          <div
            class="custom_groupcells_item"
            v-for="(sitem, sindex) in item.data"
            :key="'groupcells_item_index_' + sindex"
          >
            <van-swipe-cell>
              <van-cell
                :title="sitem.title"
                :value="sitem.desc"
                :label="sitem.label"
                :is-link="true"
                :icon="sitem.icon"
                :url="sitem.url || 'javascript:;'"
                :to="sitem.router || ''"
              ></van-cell>
              <template #right>
                <van-button
                  square
                  :text="$t('lang.删除')"
                  type="danger"
                  class="delete-button"
                  @click="removeItem(index, sindex)"
                />
              </template>
            </van-swipe-cell>
          </div>
        </van-cell-group>
      </lazy-component>
    </div>
    <div v-else class="custom_groupcells_content nodata">
      <span>{{ $t("lang.暂无数据") }}</span>
    </div>
    <div
      class="loadmore"
      v-if="!isDownLoadMore && !isLoading && listData.length > 0"
    >
      <a href="javascript:;" @click="loadMoreClickHandle">{{
        $t("lang.点击加载更多")
      }}</a>
    </div>
    <div class="loading" v-if="isLoading">
      <van-loading size="24px">{{ $t("lang.加载中") }}</van-loading>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "customGroupCells",
  props: {
    item: {
      type: Object,
      default() {
        return {};
      },
    },
    isDownLoadMore: {
      type: Boolean,
      default() {
        return false;
      },
    },
  },
  data() {
    return {
      isLoading: false,
      listData: [],
    };
  },
  created() {
    this.requestData();
  },
  mounted() {
    let that = this;
    if (this.$props.isDownLoadMore) {
      this.$query(window).on("scroll", function(e) {
        that.scrollHandle(e);
      });
    }
  },
  methods: {
    loadMoreClickHandle() {
      this.requestData(this.$props.item, true);
    },
    requestData(v, bool) {
      if (!this.isLoading) {
        this.isLoading = true;
        this.listData = bool
          ? this.listData.concat(this.$props.item.data)
          : this.$props.item.data;
        this.isLoading = false;
      }
    },
    scrollHandle(e) {
      let that = this;
      let top = e.target.documentElement.scrollTop,
        height = screen.height,
        scrollHeight = e.target.documentElement.scrollHeight;
      that.listPinterestTimeout && clearTimeout(that.listPinterestTimeout);
      that.listPinterestTimeout = setTimeout(() => {
        if (top + height + 60 >= scrollHeight) {
          that.requestData(that.$props.item, true);
        }
      }, 500);
    },
    clearItemList(item, index) {
      let that = this;
      that.listData.splice(index, 1);
      Toast({
        message: this.$t("lang.组清空"),
        position: "bottom",
      });
    },
    removeItem(index, sindex) {
      let that = this;
      that.listData[index].data.splice(sindex, 1);
      Toast({
        message: this.$t("lang.单元清空"),
        position: "bottom",
      });
    },
  },
  beforeDestroy() {
    let that = this;
    that.listPinterestTimeout && clearTimeout(that.listPinterestTimeout);
    that.$query(window).off("scroll");
  },
};
</script>
<style lang="less">
@base: 23.44rem;
.custom_groupcells {
  margin: (10 / @base) 0 0;
  text-align: left;
  .custom_groupcells_content {
    &.nodata {
      span {
        display: block;
        text-align: center;
        font-size: (14 / @base);
        margin: 0 auto;
        line-height: (100 / @base);
      }
    }
    .van-sticky--fixed {
      background-color: #fff;
      padding: 0 (15 / @base);
      border-bottom: (1 / @base) solid #efefef;
      text-align: center;
      .custom_groupcells_content_title {
        background-color: #fff;
        padding: (15 / @base) 0;
        color: #333;
        .custom_groupcells_content_title_left {
          width: 80%;
        }
        .custom_groupcells_content_title_right {
          width: 20%;
          color: #999;
        }
      }
    }
    .custom_groupcells_content_title {
      background-color: #fff;
      padding: (10 / @base) 0;
      display: flex;
      .custom_groupcells_content_title_left {
        width: 85%;
      }
      .custom_groupcells_content_title_right {
        width: 15%;
        .van-icon {
          vertical-align: middle;
        }
      }
    }
  }
  .delete-button {
    height: 100%;
  }
  .loading {
    padding: (10 / @base) 0 (20 / @base) 0;
  }
  .loadmore {
    font-size: (14 / @base);
    margin: (5 / @base);
    text-align: center;
    /*     background-color: #fff;
    box-shadow: 0 2px 12px rgba(100, 101, 102, 0.12);
 */
    border-radius: (2.5 / @base);
    a {
      padding: (10 / @base);
      color: #999;
      display: block;
    }
  }
}
</style>
